
var crypto = require('crypto')

function getSha25Sign(args,key) {

    var keys = Object.keys(args).sort();

    let signList = [];
    keys.map((v) => {
        signList.push({
            key:v,
            value:args[v]
        })
    });
    console.log("signList:",signList);

    var signTemp = '';
    signList.map( (v) => {
        signTemp += `${v.key}=${v.value}&`;
    })
    console.log('signTemp:',signTemp);
    signTemp += ('key=' + key);
    var sign = crypto.createHmac('SHA256',key).update(signTemp).digest('hex').toUpperCase();
    return sign;
}

function md5(text) {
    return crypto.createHash('md5').update(text).digest('hex');
};

function getMd5Sign(args,appSecret) {

    var keys = Object.keys(args).sort();

    let signList = [];
    keys.map((v) => {
        signList.push({
            key:v,
            value:args[v]
        })
    });
    console.log("signList:",signList);

    var signTemp = '';
    signList.map( (v) => {
        signTemp += `${v.value}`;
    })
    console.log('signTemp:',signTemp);
    signTemp += (appSecret);
    var sign = crypto.createHash('md5').update(signTemp).digest('hex');
    return sign;
}


module.exports = {
    md5,
    getSha25Sign,
    getMd5Sign
}